package com.example.retrofitdemo

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import cbfg.http.RequestStatus
import cbfg.http.errorMsg
import cbfg.http.flowRequest
import com.example.retrofitdemo.databinding.ActivityMainBinding
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
    // 利用系统扩展的代理，快速生成viewModel
    private val viewModel by viewModels<MainViewModel>()

    private lateinit var viewBinding: ActivityMainBinding

    private val mAdapter = MainRvAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)

        viewBinding.recyclerView.adapter = mAdapter

        // 定义LiveData
        viewModel.newsLiveData.observe(this) {
            when (it.requestStatus) {
                RequestStatus.START -> {
                }

                RequestStatus.SUCCESS -> {
                    it.data?.let { newsBean ->
                        // 直接刷新界面
                        mAdapter.setList(newsBean.result)
                    }
                }

                RequestStatus.ERROR -> {
                    Log.e("MainActivity", "请求出错:${it.error}")
                    Toast.makeText(this, "网络出错了,${it.error}", Toast.LENGTH_SHORT).show()
                }

                RequestStatus.COMPLETE -> {
                    Toast.makeText(this, "网络请求完成了", Toast.LENGTH_SHORT).show()
                }
            }
        }

        viewModel.articleLiveData.observe(this) {
            when (it.requestStatus) {
                RequestStatus.START -> {
                }

                RequestStatus.SUCCESS -> {
                    Log.e("MainActivity", "请求成功:${it.data}")
                }

                RequestStatus.ERROR -> {
                    Log.e("MainActivity", "请求出错:${it.error}")
                    Toast.makeText(this, "网络出错了,${it.error}", Toast.LENGTH_SHORT).show()
                }

                RequestStatus.COMPLETE -> {
                    Toast.makeText(this, "网络请求完成了", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    override fun onStart() {
        super.onStart()
        // 在你需要数据的地方，调用方法获取数据
        //viewModel.getNews()
        viewModel.getArticles()

        lifecycleScope.launch {
            val job = lifecycleScope.launch {
                flowRequest({ viewModel.newsApi.getArticleList() }) { data, error ->
                    if (error != null) {
                        Log.e("请求错误", "错误信息：${error.errorMsg}")
                    } else if (data == null) {
                        Log.e("请求错误", "暂无数据")
                    } else {
                        Log.e("请求成功", "result：${data}")
                    }
                }
            }
            /*lifecycleScope.launch {
                delay(100)
                job.cancel()
            }*/
        }
    }
}


